package com.ruoyi.web.controller.TiebaShares.controller;

import java.util.List;
import java.util.Arrays;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.controller.TiebaPosts.domain.TiebaPosts;
import com.ruoyi.web.controller.TiebaPosts.mapper.TiebaPostsMapper;
import com.ruoyi.web.controller.TiebaShares.domain.TiebaShares;
import com.ruoyi.web.controller.TiebaShares.service.ITiebaSharesService;
import com.ruoyi.web.controller.TiebaShares.vo.TiebaSharesReqAdd;
import com.ruoyi.web.controller.TiebaShares.vo.TiebaSharesReqList;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分享的表的情况Controller
 *
 * @author ruoyi
 * @date 2025-03-09
 */
@Api(tags = "用户-分享")
@RestController
@RequestMapping("/TiebaShares/TiebaShares")
public class TiebaSharesController extends BaseController {
    @Autowired
    private ITiebaSharesService tiebaSharesService;

    @Resource
    private TiebaPostsMapper postsMapper;

    /**
     * 查询分享的表的情况列表
     */
    @ApiOperation("分享的列表")
    @PreAuthorize("@ss.hasPermi('TiebaShares:TiebaShares:list')")
    @GetMapping("/list")
    public TableDataInfo list(TiebaSharesReqList reqList) {
        startPage();
        TiebaShares tiebaShares = new TiebaShares();
        tiebaShares.setUserId(getUserId());
        tiebaShares.setDeptId(getDeptId());
        tiebaShares.setPostId(reqList.getFromId());
        List<TiebaShares> list = tiebaSharesService.selectTiebaSharesList(tiebaShares);
        return getDataTable(list);
    }


    /**
     * 新增分享的表的情况
     */
    @ApiOperation("分享用户")
    @PreAuthorize("@ss.hasPermi('TiebaShares:TiebaShares:add')")
    @Log(title = "分享的表的情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TiebaSharesReqAdd sharesReqAdd) {
        TiebaShares tiebaShares = new TiebaShares();
        tiebaShares.setUserId(getUserId());
        tiebaShares.setDeptId(getDeptId());
        tiebaShares.setPostId(sharesReqAdd.getFormId());
        tiebaShares.setOtherUserId(sharesReqAdd.getShareUserId());
        boolean isSaved = tiebaSharesService.save(tiebaShares);
        if (isSaved) {
            // 计算帖子评论数量
            TiebaPosts posts = postsMapper.selectById(sharesReqAdd.getFormId());
            if (posts != null) {
                Long shareCount = posts.getShareCount();
                // 如果 commentCount 为 null，初始化为 0
                if (shareCount == null) {
                    shareCount = 0L;
                }
                posts.setShareCount(shareCount + 1);
                postsMapper.updateById(posts);
            }
            return AjaxResult.success(isSaved);
        }
        return AjaxResult.success(false);
    }

}
